﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.product
{
    public partial class deliveryRegion : BaseAdmin
    {
        readonly DALRegion theRegion = new DALRegion();
        readonly DALDeliveryRegionRef theDeliveryRegionRef = new DALDeliveryRegionRef();
        tbDeliveryRegionRef oDeliveryRegionRef;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindTreeMenu();
            }
        }

        protected void Tree1_NodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                Tree1.CheckAllNodes(e.Node.Nodes);
            }
            else
            {
                Tree1.UncheckAllNodes(e.Node.Nodes);
            }

        }

        void BindTreeMenu()
        {
            DataTable table = theRegion.GetList("depth in (1,2) ").Tables[0];

            this.BindTreeNode("-1", "0", "全国",0);
            foreach (DataRow dr in table.Rows)
            {
                if (dr["depth"].ToString() == "1")
                {
                    this.BindTreeNode(dr["parentRegion"].ToString(), dr["id"].ToString(), dr["regionName"].ToString(),1);
                }
            }
            foreach (DataRow dr in table.Rows)
            {
                if (dr["depth"].ToString() == "2")
                {
                    this.BindTreeNode(dr["parentRegion"].ToString(), dr["id"].ToString(), dr["regionName"].ToString(),2);
                }
            }
        }



        void BindTreeNode(string parentId,  string subId, string subName,int depth)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.EnableCheckBox = true;
                node.SingleClickExpand = true;
                node.Text = subName;
                node.AutoPostBack = true;
                node.Expanded = true;
                this.Tree1.Nodes.Add(node);
                nodeIndex = FindNodeById(parentId);
            }

            else
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.EnableCheckBox = true;
                node.AutoPostBack = true;
              
              
                node.Text = subName;
                if (depth == 1)
                {
                    node.SingleClickExpand = true;
                    node.Expanded = false;
                    this.Tree1.Nodes[nodeIndex].Nodes.Add(node);
                }
                else if(depth==2)
                {
                    node.SingleClickExpand = false; 
                   // node.IconUrl = "/admin/images/Editor/table_cf.gif";
                    this.Tree1.Nodes[0].Nodes[nodeIndex].Nodes.Add(node);
                }
            }

        }

        int FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.Tree1.Nodes.Count; i++)
            {
                if (this.Tree1.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
                else
                {
                    for (int m = 0; m < this.Tree1.Nodes[i].Nodes.Count; m++)
                    {
                        if (Tree1.Nodes[i].Nodes[m].NodeID == sNodeId)
                        {
                            return m;
                        }
                    }
                }
            }
            return -1;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int tempLateId = int.Parse(Request.QueryString["id"]);
            theDeliveryRegionRef.DeleteByDeliveryTemplateId(tempLateId);

            foreach (ExtAspNet.TreeNode node in this.Tree1.Nodes[0].Nodes)
            {
                if (node.Checked)
                {
                    oDeliveryRegionRef = new tbDeliveryRegionRef();
                    oDeliveryRegionRef.deliveryTemplateId = tempLateId;
                    oDeliveryRegionRef.regionId = int.Parse(node.NodeID);
                    theDeliveryRegionRef.Add(oDeliveryRegionRef);
                }
                foreach (ExtAspNet.TreeNode nodeSub in node.Nodes)
                {
                    if (nodeSub.Checked)
                    {
                        oDeliveryRegionRef = new tbDeliveryRegionRef();
                        oDeliveryRegionRef.deliveryTemplateId = tempLateId;
                        oDeliveryRegionRef.regionId = int.Parse(nodeSub.NodeID);
                        theDeliveryRegionRef.Add(oDeliveryRegionRef);
                    }
                }
            }
           
    

            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }

    }
}